package com.klangner.move.path
{
  import com.klangner.geom.Vector2D;
  
  
  public class RectanglePath implements IPath
  {
    private static var TOP:int = 1;
    private static var RIGHT:int = 2;
    private static var BOTTOM:int = 3;
    private static var LEFT:int = 4;

    private var _left:Number;
    private var _right:Number;
    private var _top:Number;
    private var _bottom:Number;
    private var _speed:Number = 10;
    private var _segment:int;
    private var _position:Vector2D;
    
    
    public function RectanglePath(left:Number, top:Number, right:Number, bottom:Number){
      _left = left;
      _right = right;
      _top = top;
      _bottom = bottom;
      _segment = TOP;
      _position = new Vector2D(_left, _top);
    }

    public function move():void{
      
      if(TOP == _segment){
        _position.x += _speed;
        if(_position.x > _right){
          _position.x = _right;
          _segment = RIGHT;
        }
      
      }else if(RIGHT == _segment){
        _position.y += _speed;
        if(_position.y > _bottom){
          _position.y = _bottom;
          _segment = BOTTOM;
        }
      
      }else if(BOTTOM == _segment){
        _position.x -= _speed;
        if(_position.x < _left){
          _position.x = _left;
          _segment = LEFT;
        }
      
      }else if(LEFT == _segment){
        _position.y -= _speed;
        if(_position.y < _top){
          _position.y = _top;
          _segment = TOP;
        }
      
      }
    }
    
    public function get position():Vector2D{
      
      return _position.clone();
    }
    
    public function get angle():Number{
      
      switch(_segment){
        case TOP:
          return 0;
          
        case RIGHT:
          return Math.PI/2;
          
        case BOTTOM:
          return Math.PI;
          
        case LEFT:
        default:
          return 3*Math.PI/2;
      }
    }
    
  }
}